export const state = () => ({
})

export const mutations = {
}

export const actions = {
    getWordList({}, name, keywords) {
        name = 'galaxy'
        return this.$api.search.getWordList(name,keywords).then((res) => {
            if(res.data.status === 'OK') {
                return Promise.resolve(res.data.data)
            }
            else {
                return Promise.reject(res.data.msg)
            }
        }, (error) => {
            return Promise.reject(error)
        }).catch((error) => {
            return Promise.reject(error)
        })
    },
    
    getAccountByUsername({}, username) {
        return this.$api.account.getAccountByUsername(username).then((res) => {
            if(res.data.status === 'OK') {
                return Promise.resolve(res.data.data)
            }
            else {
                return Promise.reject('NOK')
            }
        }, (error) => {
            return Promise.reject(error)
        }).catch((error) => {
            return Promise.reject(error)
        })
    },
    register({}, account) {
        return this.$api.account.register(account).then((res) => {
            if(res.data.status === 'OK') {
                return Promise.resolve('OK')
            }
            else {
                return Promise.reject(new Error(res.data.msg))
            }
        }, (error) => {
            return Promise.reject(error)
        }).catch((error) => {
            return Promise.reject(error)
        })
    },
    getAccountByUsernameAndPassword({commit}, {username, password}) {
        return this.$api.account.getAccountByUsernameAndPassword(username, password).then((res) => {
            if(res.data.status === 'OK') {
                commit('setSignIn', true)
                commit('setAccount', res.data.data)
                return Promise.resolve('success')
            }
            else {
                return Promise.reject(res.data.msg)
            }
        }, (error) => {
            return Promise.reject(error)
        }).catch((error) => {
            return Promise.reject(error)
        })
    },
    updateAccount({commit}, account) {
        return this.$api.account.updateAccount(account).then((res) => {
            if(res.data.status === 'OK') {
                return Promise.resolve('OK')
            }
            else {
                return Promise.reject(new Error(res.data.msg))
            }
        }, (error) => {
            return Promise.reject(error)
        }).catch((error) => {
            return Promise.reject(error)
        })
    },
    logIn({commit}, {username,password}) {
        return this.$api.account.logIn(username, password).then((res) => {
            if(res.data.status === 'OK') {
                commit('setSignIn', true)
                commit('setAccount', res.data.data)
                return Promise.resolve('success')
            }
            else {
                return Promise.reject(res.data.msg)
            }
        }, (error) => {
            return Promise.reject(error)
        }).catch((error) => {
            return Promise.reject(error)
        })
    },
}